﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MouseGesturesAddIn {
	public partial class SelectActionForm : Form {
		public SelectActionForm() {
			InitializeComponent();
		}

		/// <summary>
		/// Load Configuration - fills tree view with available actions
		/// </summary>
		/// <param name="actions">List of all available actions</param>
		/// <param name="commonActions">List of common actions</param>
		public void LoadConfiguration(List<string> actions, List<string> common) {
			actions.Sort();
			common.Sort();

			// Create common actions node
			TreeNode commonActions = new TreeNode("Common actions");
			commonActions.Name = "CommonCommands";
			treeViewActions.Nodes.Add(commonActions);

			fillActionsTree(common, commonActions.Nodes);

			// Creates All Actions node
			TreeNode allActions = new TreeNode("All actions");
			allActions.Name = "AllActions";
			treeViewActions.Nodes.Add(allActions);
			fillActionsTree(actions, allActions.Nodes);
		}

		/// <summary>
		/// Fill the node of tree with actions. Actions are organized into hierarchy, the dot is used as delimter.
		/// </summary>
		/// <param name="data">List of actions</param>
		/// <param name="root">The root node</param>
		public void fillActionsTree(List<string> data, TreeNodeCollection root) {
			foreach (string action in data) {
				if (action == "") continue;

				string[] commandHierarchy = action.Split('.');

				TreeNodeCollection category = root;
				for (int i = 0; i < commandHierarchy.Length; i++) {
					int categoryIndex = category.IndexOfKey(commandHierarchy[i]);

					if (categoryIndex == -1) {
						TreeNode tn = new TreeNode(commandHierarchy[i]);
						tn.Name = String.Join(".",commandHierarchy, 0, i + 1);
						category.Add(tn);
						category = tn.Nodes;
					}
					else {
						category = category[categoryIndex].Nodes;
					}
				}
			}
		}

		/// <summary>
		/// Gets the name of selected action
		/// </summary>
		/// <returns>Returns full name of the selected action</returns>
		public string getSelectedAction() {
			if (treeViewActions.SelectedNode.Nodes.Count > 0)
				return null;

			string actionName = treeViewActions.SelectedNode.FullPath;
			if (actionName.StartsWith("All actions."))
				return actionName.Remove(0, 12);				// 12 is length of string All Actions.
			else
				return actionName.Remove(0, 15);				// 15 is length of string Common Actions.
		}

		public bool SelectAction(string action) {
			string[] actionHierarchy = action.Split('.');

			TreeNode[] searchResult = treeViewActions.Nodes.Find(action, true);
			if (searchResult.Length > 0) {
				treeViewActions.SelectedNode = searchResult[0];
				return true;
			}
			else
				return false;
		}

		private void treeViewActions_BeforeSelect(object sender, TreeViewCancelEventArgs e) {
			// Disables OKBtn if selectes node has children - we can select "leafs" of the tree only
			if (e.Node.Nodes.Count > 0)
				btnOK.Enabled = false;
			else
				btnOK.Enabled = true;
		}

		private void treeViewActions_DoubleClick(object sender, EventArgs e) {
			if (treeViewActions.SelectedNode.Nodes != null && treeViewActions.SelectedNode.Nodes.Count > 0)
				return;

			DialogResult = DialogResult.OK;
		}



	}
}
